package com.habib.opsbd.imei;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DeviceIdActivity extends Activity {
    /** Called when the activity is first created. */
	
	Button btnFindIMEI;
	TextView txtIMEI;
	String deviceID = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnFindIMEI = (Button)findViewById(R.id.BtnDeviceId);
        txtIMEI =(TextView)findViewById(R.id.TxtDeviceId);
        
       

        btnFindIMEI.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String androidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
				findDeviceID();
				String compareIMEI = "123456789";
/*				if (deviceID!=null) {
					Toast.makeText(getBaseContext(), "Your device ID is = " + deviceID, Toast.LENGTH_LONG).show();
				txtIMEI.setText(deviceID);	
				//Toast.makeText(getBaseContext(), "Your device ID is = " + androidID, Toast.LENGTH_LONG).show();
				}*/
				//Toast.makeText(getBaseContext(), "Your compare IMEI is = " + compareIMEI, Toast.LENGTH_LONG).show();
/*				if (deviceID.equals(compareIMEI)) {
					Toast.makeText(getBaseContext(), "Your device ID is = " + deviceID, Toast.LENGTH_LONG).show();
				}*/
				
				
				TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
				String imsi = mTelephonyMgr.getSubscriberId();
				String imei = mTelephonyMgr.getDeviceId();
				Toast.makeText(getApplicationContext(), "IMSI: "+imsi+" IMEI: "+imei, Toast.LENGTH_LONG).show();
			}
		});
    }
    public String findDeviceID() {
        
        String serviceName = Context.TELEPHONY_SERVICE;
        TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
        int deviceType = m_telephonyManager.getPhoneType();
        switch (deviceType) {
              case (TelephonyManager.PHONE_TYPE_GSM):
              break;
    // case (TelephonyManager.PHONE_TYPE_CDMA):
     //break;
     case (TelephonyManager.PHONE_TYPE_NONE):
     break;
             default:
            break;
        }
        deviceID = m_telephonyManager.getDeviceId();
        return deviceID;
   }
    public String getInstallationId( Context ctx )
    {
      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences( ctx );
      
      if ( settings == null )
        return "UNKNOWN";

      if ( settings.contains( "InstallId" ) )
        return settings.getString( "InstallId", "UNKNOWN" );

      SharedPreferences.Editor editor = settings.edit();  

      if ( editor == null )
        return "UNKNOWN";

      String id = "ID" + new Random().nextInt( 99999999 );

      editor.putString( "InstallId", id );
      editor.commit();
      
      return id;  
    }
}